package com.way.demo.comm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Set;

/**
 * @author lipingwei
 * @create 2021-01-21 15:13
 * 说明：@RestControllerAdvice 只能拦截controller 层的注解，对于过滤器，或是拦截器的异常无能为力
 *
 */
//@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 入参校验的异常
     * @param e
     * @return
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public R handleMAException(MethodArgumentNotValidException e) {
        R r = new R();
        String validatorMessage = "";
        BindingResult bindingResult = e.getBindingResult();
        ObjectError error;
        if (bindingResult.hasErrors()) {
            for(Iterator var5 = bindingResult.getAllErrors().iterator(); var5.hasNext(); validatorMessage = error.getDefaultMessage()) {
                error = (ObjectError)var5.next();
            }
        }
        log.warn(e.getMessage());
        r.setSuccess("false")
                .setCode("validator_error")
                .setMsg(validatorMessage);
        return r;
    }

    /**
     *  入参校验异常
     * @param e
     * @return
     */
    @ExceptionHandler({ConstraintViolationException.class})
    public R handleCVException(ValidationException e) {
        R r = new R();
        String validatorMessage = "";
        if (e instanceof ConstraintViolationException) {
            ConstraintViolationException exs = (ConstraintViolationException)e;
            Set<ConstraintViolation<?>> constraintViolations = exs.getConstraintViolations();
            Iterator var6 = constraintViolations.iterator();
            if (var6.hasNext()) {
                ConstraintViolation<?> item = (ConstraintViolation)var6.next();
                validatorMessage = item.getMessage();
            }
        }
        log.warn(e.getMessage());
        r.setCode("validator_error")
                .setSuccess("false")
                .setMsg(validatorMessage);
        return r;
    }

    /**
     *  业务类异常
     * @param e
     * @return
     */

    @ExceptionHandler({BusinessException.class})
    public R handleBusException(BusinessException e) {
        R r = new R();
        r.setSuccess("false")
                .setCode(e.getCode())
                .setMsg(e.getMsg());
        return r;
    }

    @ExceptionHandler({Exception.class})
    public R handleException(Exception e) {
        R r = new R();
        r.setSuccess("false");
        r.setCode("-1");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        String exception = baos.toString();
        log.error(exception);
        r.setMsg("未知系统异常");
        return r;
    }

    @ExceptionHandler({RuntimeException.class})
    public R handleException(RuntimeException e) {
        R r = new R();
        log.error(e.getMessage(), e);
        r.setSuccess("false");
        r.setCode("-1");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        String exception = baos.toString();
        log.error(exception);
        r.setMsg("未知系统异常");
        return r;
    }





//    @ExceptionHandler(NoHandlerFoundException.class)
//    public R handlerNoFoundException(NoHandlerFoundException e) {
//        R r = new R();
//        log.error(e.getMessage(), e);
//        r.setSuccess("false");
//        r.setCode("404");
//        r.setMsg("路径不存在,请检查路径是否正确");
//        return r;
//
//    }

}
